﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Licenta_Stomatologie.Panels
{
    public partial class PacientCreatePanel : UserControl
    {
        public PacientCreatePanel()
        {
            InitializeComponent();
        }

        private void createPacient()
        {
            if (!checkData())
            {
                MessageBox.Show(Helpers.ErrorMessages.ERR_EMPTY_FIELD);
                return;
            }

            ProjectDBEntities db = new ProjectDBEntities();

            Pacient pac = new Pacient();

            pac.cnp = tboxCNP.Text;
            pac.nume = tboxNume.Text;
            pac.prenume = tboxPrenume.Text;
            pac.sex = cboxSex.SelectedItem.ToString();
            pac.dataNasterii = dtpDOB.Value;
            pac.adresa = tboxAdresa.Text;
            pac.telefonFix = tboxTelFix.Text;
            pac.telefonMobil = tboxTelMobil.Text;
            pac.eMail = tboxEmail.Text;
            pac.localitate = tboxLocalitate.Text;
            pac.judet = cboxJudet.SelectedItem.ToString();
            pac.dataCreerii = DateTime.Now;

            db.Pacients.Add(pac);

            db.SaveChanges();

            MessageBox.Show(Helpers.UserMessages.SUCCES_CREATE_PACIENT);
        }

        private bool checkData()
        {
            foreach (TextBox tb in this.Controls.OfType<TextBox>())
            {
                if (tb.Text.Length == 0)
                {
                    return false;
                }
            }
            foreach (ComboBox cb in this.Controls.OfType<ComboBox>())
            {
                if (((String)cb.SelectedItem).Length == 0)
                {
                    return false;
                }
            }
            return true;
        }

        private void btnSaveChanges_Click(object sender, EventArgs e)
        {
            createPacient();

            this.Controls.Clear();
            this.Controls.Add(new Panels.Dashboard());
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show(Helpers.UserMessages.PACIENT_EDIT_CANCEL, "Confirmare", MessageBoxButtons.OKCancel);

            if (result == DialogResult.OK)
            {
                this.Controls.Clear();
                this.Controls.Add(new Panels.Dashboard());
            }
        }
    }


}
